package ct.zxd.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ct.zxd.db.Dao;
import ct.zxd.db.OrderDao;

public class EditOrder extends HttpServlet {
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {	
		doPost(request,response);	
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;character=utf-8");
        response.setHeader("Cache-Control", "no-cache");
		request.setCharacterEncoding("utf-8");
		
		PrintWriter writer = response.getWriter();
		String shop = request.getParameter("shop");
		String id = request.getParameter("ID");
		int orderid = Integer.parseInt(id);
		String shipper = request.getParameter("shipper");
		String code = request.getParameter("code");
		String status = request.getParameter("status");
		System.out.println("sta "+status);
		try{
			OrderDao dao = new OrderDao();
			if(id==null){
//				String selectType = request.getParameter("selectType");
				String selectStatus = request.getParameter("selectStatus");
//				String orderid = request.getParameter("orderid");
				String start = request.getParameter("start");
				String end = request.getParameter("end");
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				Date startDate = formatter.parse(start);
				long starttime = startDate.getTime();
				Date endDate = formatter.parse(end);
				long endtime = endDate.getTime();
				
				String condition = "where shop_id="+shop;
//				if(selectType!=null && !selectType.equals(""))condition+=" and type='"+selectType+"'";
				if(selectStatus!=null && !selectStatus.equals(""))condition+=" and status='"+selectStatus+"'";
//				if(orderid!=null && !orderid.equals(""))condition+=" and order_id="+orderid;
				condition+=" and time>="+starttime;
				condition+=" and time<="+endtime;

				request.getRequestDispatcher("book.jsp?shopid="+shop+"&condition="+condition).forward(request, response);   

			}else{
				int count = 0;
				count = dao.updateOrderLogistics(orderid, shop, shipper, code);
				if (status.equals("2")) {
					dao.updateOrderStatus(orderid, shop, 3);
				}
//				if(flag!=null){
					//count = dao.updateOrderStatus(showid,shop);
//				}else{
					//count = dao.deleteOrder(showid,shop);
//				}	
				if(count<=0){
					writer.write("数据库出错,请联系管理员");
				}else {
					writer.write("发货成功");
				}
			}
			
		}catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println(e);
			writer.write("服务器连接错误");
		}	
	}

}
